package com.chinairi.mall.service.impl;

import com.chinairi.common.utils.IdGenerator;
import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.mall.controller.viewobject.PurInStorage;
import com.chinairi.mall.controller.viewobject.PurOutStorage;
import com.chinairi.mall.controller.viewobject.SignData;
import com.chinairi.mall.controller.viewobject.StAddress;
import com.chinairi.mall.dao.*;
import com.chinairi.mall.model.*;
import com.chinairi.mall.service.IPurchasePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 模拟入库业务逻辑层实现类
 * </p>
 * @author lxw
 * @since 2020-10-20
 */
@Service
public class PurchasePlanServiceImpl implements IPurchasePlanService {

    @Autowired
    private ProjectPlanReqDetailMapper projectPlanReqDetailMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderGoodsMapper orderGoodsMapper;

    @Autowired
    private ProjectPlanPickingDetailMapper projectPlanPickingDetailMapper;

    @Autowired
    private ProjectPlanPurchaseTransportMapper projectPlanPurchaseTransportMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, Object> savePurchaseInStorage(PurInStorage purInStorage){
        List<Map<String,Object>> list=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        int toNum=new Random().nextInt(purInStorage.getPurNum())+1;
        map.put("orderNo",purInStorage.getOrderNo());
        map.put("purNum",purInStorage.getPurNum());
        map.put("matNo",purInStorage.getMatNo());
        map.put("toNum",toNum);
        list.add(map);
        OrderGoods orderGoods = new OrderGoods();
        OrderInfo orderInfo = new OrderInfo();
        String ogId = IdGenerator.UUID();
        orderGoods.setOgId(ogId);
        orderGoods.setOrderId(purInStorage.getOrderNo());
        orderGoods.setOgGoodsNumber(purInStorage.getPurNum().floatValue());
        orderGoods.setGoodsDbid(purInStorage.getMatNo());
        orderGoods.setOgStorageNumber((float) toNum);
        orderInfo.setOrderId(purInStorage.getOrderNo());
        orderInfo.setOrderStorageNumber((float) toNum);
        try {
            orderInfoMapper.insert(orderInfo);
            orderGoodsMapper.insert(orderGoods);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.toWarningMessage("保存失败", false);
        }
        return ResponseUtil.toSuccessMessage("保存成功", true);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, Object> savePurchaseOutStorage(PurOutStorage purOutStorage){
        List<Map<String,Object>> list=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        int actualPickNum=new Random().nextInt(purOutStorage.getPickNum())+1;
        ProjectPlanReqDetail projectPlanReqDetail = new ProjectPlanReqDetail();
        OrderInfo orderInfo = new OrderInfo();
        OrderGoods orderGoods = new OrderGoods();
        ProjectPlanPickingDetail projectPlanPickingDetail = new ProjectPlanPickingDetail();
        map.put("reqNo",purOutStorage.getReqNo());
        map.put("orderNo",purOutStorage.getOrderNo());
        map.put("pickNo",purOutStorage.getPickNo());
        map.put("matNo",purOutStorage.getMatNo());
        map.put("pickNum",purOutStorage.getPickNum());
        map.put("actualPickNum",actualPickNum);
        list.add(map);
        String ogId = IdGenerator.UUID();
        orderGoods.setOgId(ogId);
        orderGoods.setOrderId(purOutStorage.getOrderNo());
        orderGoods.setOgDeliveryNumber((float) actualPickNum);
        orderGoods.setGoodsDbid(purOutStorage.getMatNo());
        String planDetailId = IdGenerator.UUID();
        projectPlanReqDetail.setPlanDetailId(planDetailId);
        projectPlanReqDetail.setPlanReqId(purOutStorage.getReqNo());
        orderInfo.setOrderId(purOutStorage.getOrderNo());
        orderInfo.setGmId(purOutStorage.getPickNo());
        orderInfo.setOrderDeliveryNumber((float) actualPickNum);
        String pppdDbid = IdGenerator.UUID();
        projectPlanPickingDetail.setPppdDbid(pppdDbid);
        projectPlanPickingDetail.setPppdPickNumber(purOutStorage.getPickNum().floatValue());
        try {
            orderGoodsMapper.insert(orderGoods);
            orderInfoMapper.insert(orderInfo);
            projectPlanReqDetailMapper.insert(projectPlanReqDetail);
            projectPlanPickingDetailMapper.insert(projectPlanPickingDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.toWarningMessage("保存失败",false);
        }
        return ResponseUtil.toSuccessMessage("保存成功", true);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, Object> saveSignInData(SignData signData){
        List<Map<String,Object>> list=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createDate = new Date();
        map.put("transportNo",signData.getTransportNo());
        map.put("signPerson","李传忠");
        map.put("signTime",sdf.format(new Date()));
        map.put("matList",signData.getMatList());
        list.add(map);
        ProjectPlanPurchaseTransport projectPlanPurchaseTransport = new ProjectPlanPurchaseTransport();
        String transportId = IdGenerator.UUID();
        projectPlanPurchaseTransport.setTransportId(transportId);
        projectPlanPurchaseTransport.setCreateTime(createDate);
        projectPlanPurchaseTransport.setReceiveMan("李传忠");
        try {
            projectPlanPurchaseTransportMapper.insert(projectPlanPurchaseTransport);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.toWarningMessage("保存失败",signData.getMatList());
        }
        return ResponseUtil.toSuccessMessage("保存成功", signData.getMatList());
    }

}
